package com.imy.luascript.app.ui;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.imy.accessibility.CoreService;
import com.imy.accessibility.utils.AccessibilityUtils;
import com.imy.common.constant.Constant;
import com.imy.common.util.ALog;
import com.imy.common.util.FileUtils;
import com.imy.common.util.SPUtil;
import com.imy.common.util.ToastUtil;
import com.imy.common.util.UiUtil;
import com.imy.lua.helper.properties.PropertiesLoaderHelper;
import com.imy.lua.utils.ProjectUtils;
import com.imy.luascript.R;
import com.imy.luascript.app.ui.main.MainActivity;
import com.imy.luascript.base.activity.BaseViewActivity;
import com.imy.luascript.constant.SPConstant;
import com.imy.luascript.utils.ToolBarBuilder;
import com.imy.luascript.viewmodel.BaseViewModel;
import com.imy.touch.TouchCore;

import java.io.File;
import java.util.List;

public class PermissionActivity extends BaseViewActivity {
    private int rootRetry = 0;
    private int MAX_TRY = 3;
    private Handler handler = new Handler(Looper.getMainLooper());

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected ToolBarBuilder getToolBarBuilder() {
        ToolBarBuilder.Builder builder = new ToolBarBuilder.Builder();
        builder.setHasToolbar(false);
        return builder.build();
    }

    private TouchCore.InitCallBack callBack = new TouchCore.InitCallBack() {
        @Override
        public void onSuccess() {
            SDPermissionCheck();
        }

        @Override
        public void onFail(boolean isRoot) {
            if (isRoot) {
                if (rootRetry != MAX_TRY) {
                    rootRetry++;
                    TouchCore.get().tryStartRootTouch(self, callBack);
                } else {
                    SDPermissionCheck();
                }
            } else {
                SDPermissionCheck();
            }
        }
    };

    private void SDPermissionCheck() {
        if (isFinishing()) {
            return;
        }
        XXPermissions.with(this).permission(Permission.WRITE_EXTERNAL_STORAGE)
                .permission(Permission.READ_EXTERNAL_STORAGE)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {

                        if (all) {

                            goMain();
                        } else {
                            exitApp();
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        exitApp();
                    }
                });
    }

    private void goMain() {
        //检测项目目录是否创建
        FileUtils.createRootScreenFilePath();

        boolean aBoolean = SPUtil.getBoolean(SPConstant.FIRST_LAUNCH, true);
        if (aBoolean) {
            //首次进入创建示例工程
           ProjectUtils.copyProjectToSD(self, "project", Constant.APP_NAME + "/示例工程",
                    "示例工程");
            SPUtil.setBoolean(SPConstant.FIRST_LAUNCH, false);
        }
        startActivity(new Intent(self, MainActivity.class));
        finish();
    }

    private void checkAccessibility() {
        boolean b = AccessibilityUtils.checkAccessibilityEnabled(this);
        if (b){
            TouchCore.get().init(this, callBack);
        }else {
            AccessibilityUtils.openAccessibility(this);
        }
        isChecking=false;
//        SDPermissionCheck();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isChecking) {
            return;
        }
        ALog.i("onResume: service" + CoreService.mService);
        isChecking = true;
        checkAccessibility();
    }

    private boolean isChecking = false;

    private void exitApp() {
        ToastUtil.toast(getApplicationContext(), "未授权SD卡权限，应用无法使用");
        handler.postDelayed(finishRunnable, 2000);
    }

    private Runnable finishRunnable = new Runnable() {
        @Override
        public void run() {
            finish();
        }
    };

    @Override
    protected void initView() {

    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_permission;
    }
}
